import Vue from "vue"

/**
 * `
 *  dateFormat(dateStr,"yyyy-mm-dd hh24:mi:ss")
 * `
 */
function dateFormat(dateStr, pattern = '') {
    if (!dateStr) return ''
    const date = new Date(dateStr)
    let hours = date.getHours()
    let ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours ? hours : 12;
    const time = {
        yyyy: date.getFullYear(),
        yy: date.getFullYear(),
        MM: ('0' + (date.getMonth() + 1)).slice(-2),
        dd: ('0' + date.getDate()).slice(-2),
        hh24: ('0' + date.getHours()).slice(-2),
        mi: ('0' + date.getMinutes()).slice(-2),
        ss: ('0' + date.getSeconds()).slice(-2),
        hh12: hours + ampm
    }
    return pattern.replace(/(yyyy|yy|MM|dd|hh24|mi|ss|hh12)/g, (match, p) => {
        return time[p]
    })
}

function myself(target) {
    try {
        let id = JSON.parse(localStorage.getItem('HuiLY')).userinfo.id
        if (target == id)
            return '本人'
        else return target
    } catch (error) {
        console.error(error);
    }
}



/**
 * 
 * @param {import("vue/types/umd").VueConstructor} Vue
 */
function _initFilter(Vue) {
    Vue.filter('dateFormat', dateFormat)
    Vue.filter('myself', myself)
}

export default _initFilter